package com.zzl.study.auth.security.authentication.client;

import lombok.Data;
import org.springframework.security.oauth2.core.OAuth2AccessToken;

import java.time.Instant;
import java.util.Set;

/**
 * @author: zhangzl
 * @date: 2024/6/11 15:52
 * @version: 1.0
 * @description: OAuth2 access_token
 */
@Data
public class GatewayOAuth2AccessToken {

    private String tokenValue;

    private Instant issuedAt;

    private Instant expiresAt;

    private Set<String> scopes;

    public static GatewayOAuth2AccessToken of(OAuth2AccessToken oAuth2AccessToken) {
        if (oAuth2AccessToken == null) {
            return null;
        }
        GatewayOAuth2AccessToken gatewayOAuth2AccessToken = new GatewayOAuth2AccessToken();
        gatewayOAuth2AccessToken.setTokenValue(oAuth2AccessToken.getTokenValue());
        gatewayOAuth2AccessToken.setIssuedAt(oAuth2AccessToken.getIssuedAt());
        gatewayOAuth2AccessToken.setExpiresAt(oAuth2AccessToken.getExpiresAt());
        gatewayOAuth2AccessToken.setScopes(oAuth2AccessToken.getScopes());
        return gatewayOAuth2AccessToken;
    }

    public static OAuth2AccessToken to(GatewayOAuth2AccessToken gatewayOAuth2AccessToken) {
        if (gatewayOAuth2AccessToken == null) {
            return null;
        }
        return new OAuth2AccessToken(
                OAuth2AccessToken.TokenType.BEARER,
                gatewayOAuth2AccessToken.getTokenValue(),
                gatewayOAuth2AccessToken.getIssuedAt(),
                gatewayOAuth2AccessToken.getExpiresAt(),
                gatewayOAuth2AccessToken.getScopes());
    }

}
